#!/usr/bin/env python

# Copyright 2010 G. M. Bond. All Rights Reserved.
# Licensed to PSF under a Contributor Agreement.

#Python imports
import sys
import re

import graph

#import lib2to3.pytree as pytree

#For testing, let's just import one fixer by hand. Later this should take
# a list of fixers on the command line
#from lib2to3.fixes import fix_buffer
from lib2to3.fixes import fix_next

def graph_node(graph, node):
    ##For Nodes
    #print "Doing: %s" % node.__repr__
    #print "Node ID: %d (%d children)" % (id(node), len(node.children))
    #this = node
    #this = graph.newItem("%s\\nTYPE: %s\\n%s" % (node.__class__.__name__,
    #                                       node.type_repr(),
    #                                                  re.escape(node.__unicode__())))
    #i = 0
    #for current_child in node.children:
        #i += 1
    #    child = graph_node(graph, current_child)
    #    graph.newLink(this, child)
    #print "Processed %d children from %d" % (i, id(node))
    #return this

    ##For NodePatterns
    recurseChildren = False
    nodename = ""
    #this switch might be better served by adding some sort of method to the
    #NodePattern classes that could be called polymorphically
    if isinstance(node, pytree.LeafPattern):
        nodename += "\\nclasstype: Leaf"
        if node.type is None:
            nodename += "\\nMatch type: any"
        else:
            nodename += "\\nMatch type: " + node.type_repr()
        if node.content is not None:
            nodename += "\\nMatch string: " + node.content
    elif isinstance(node, pytree.NodePattern):
        nodename += "\\nclasstype: Node"
        if node.type is None:
            nodename += "\\nMatch type: any single node"
        else:
            nodename += "\\nMatch type: " + node.type_repr()
            if node.content is not None: 
                recurseChildren = True
    elif isinstance(node, pytree.WildcardPattern):
        nodename += "\\nclasstype: Wildcard"
        if node.content is None:
            if node.max == 0x7FFFFFFF:
                nodename += "Match %d-* of any" % node.min
            else:
                nodename += "Match %d-%d of any" % (node.min, node.max)
        else:
            if len(node.content) == 1:
                recurseChildren = True
                print node.content
            #Recuse each alternative
    elif isinstance(node, pytree.NegatedPattern):
        nodename += "\\nclasstype: Negated"
        if node.content is None:
            nodename += "\\nMatch $"
        else:
            nodename += "\\nMatch if children do NOT match"
            recurseChildren = True
    else:
        print "uh oh, not a node?"

    if node.name is not None:
        nodename += "\\nStore as: " + node.name
    #if it's a leaf, include content if not empty
    #if it's a NodePattern, recurse over children
    #this = graph.newItem("type: %s\\n
    this = graph.newItem(nodename)

    if recurseChildren:
        for n in node.content:
            child = graph_node(graph, n)
            graph.newLink(this, child)
    return this

def main(argv=None):
    #Change this out once input can be taken from argv
    fix_log = []
    #target_fixers = [fix_buffer.FixBuffer(None, fix_log)]
    target_fixers = [fix_next.FixNext(None, fix_log)]

    #for showing syntax tree
    #for fixer in target_fixers:
    #    fixer.compile_sm_pattern()

    g = graph.GvGraph()
    fixer = target_fixers[0]
    #print fixer.sm_root.__repr__()
    #graph_node(graph, fixer.sm_root)
    #graph_node(graph, fixer.pattern)
    fixer.pattern.graph_node(g)
    g.output()

    #lib2to3.pytree.node
    #build_tree_graph(fixer.sm_root)
    #print target_fixers
    #print fix_log

if __name__ == '__main__':
    sys.exit(main())
